package org.example.repository;

import org.springframework.scheduling.concurrent.ExecutorConfigurationSupport;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;

public class TestExecutor {

    private final Executor executor;

    public TestExecutor(Executor executor) {
        this.executor = executor;
    }

    public void execute(Runnable... tasks) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(tasks.length);
        for (Runnable task : tasks) {
            executor.execute(() -> {
                task.run();
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
    }

    public void initialize() {
        if (executor instanceof ExecutorConfigurationSupport support) {
            support.initialize();
        }
    }

    public void shutdown() {
        if (executor instanceof ExecutorConfigurationSupport support) {
            support.shutdown();
        }
    }
}
